package System;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import java.awt.SystemColor;

public class ServerGUI extends JPanel implements ActionListener, ItemListener
{
	private JButton btnConnect;
	private JCheckBox chckbxPort;
	private JTextField txtPass;
	private JTextField txtPort;
	private JCheckBox chckbxPass;
	private final int DefaultPort = 5555;
	private final String DefaultPass = "Braude";
	private boolean passChecked = true, PortChecked = true;
	
	/**
	 * Create the panel.
	 */
	public ServerGUI() 
	{
		super();
		initialize();
	}
	
	private void initialize()
	{
		setBorder(new TitledBorder(null, "Server", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(300,200,400,361);
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Choose Port and Data Base password and click Connect");
		lblNewLabel.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 12));
		lblNewLabel.setBounds(47, 34, 341, 14);
		add(lblNewLabel);
		
		chckbxPort = new JCheckBox("Default Port");
		chckbxPort.setToolTipText("Use the default values");
		chckbxPort.setSelected(true);
		chckbxPort.setBackground(new Color(255, 228, 196));
		chckbxPort.setBounds(16, 143, 108, 23);
		chckbxPort.addItemListener(this);
		add(chckbxPort);
		
		JLabel lblPort = new JLabel("1. Port:");
		lblPort.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 12));
		lblPort.setBounds(150, 149, 56, 14);
		add(lblPort);
		
		JLabel lblPassword = new JLabel("2. Password:");
		lblPassword.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 12));
		lblPassword.setBounds(150, 190, 78, 14);
		add(lblPassword);
		
		txtPort = new JTextField();
		txtPort.setEnabled(false);
		txtPort.setEditable(false);
		txtPort.setText("5555");
		txtPort.setBounds(238, 146, 86, 20);
		add(txtPort);
		txtPort.setColumns(10);
		
		txtPass = new JTextField();
		txtPass.setEnabled(false);
		txtPass.setEditable(false);
		txtPass.setText("Braude");
		txtPass.setBounds(238, 187, 86, 20);
		add(txtPass);
		txtPass.setColumns(10);
		
		btnConnect = new JButton("CONNECT");
		btnConnect.setBounds(150, 274, 89, 23);
		btnConnect.addActionListener(this);
		add(btnConnect);
		
		chckbxPass = new JCheckBox("Default Password");
		chckbxPass.setToolTipText("Use the default values");
		chckbxPass.setSelected(true);
		chckbxPass.setBackground(new Color(255, 228, 196));
		chckbxPass.setBounds(16, 184, 128, 23);
		chckbxPass.addItemListener(this);
		add(chckbxPass);	
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String Pass;
		int Port;
		
		if (e.getSource() == btnConnect)
		{
			if (PortChecked)
				Port = DefaultPort;
			else
				Port = Integer.parseInt(txtPort.getText());
			
			if (passChecked)
				Pass = DefaultPass;
			else
				Pass = txtPass.getText();
			
			Server.setServer(Port,Pass);
		}	
	}

	@Override
	public void itemStateChanged(ItemEvent ie) 
	{
		if (ie.getSource() == chckbxPort)
		{
			if (PortChecked = chckbxPort.isSelected())
			{
				txtPort.setEnabled(false);
				txtPort.setEditable(false);
			}
			else
			{
				txtPort.setEnabled(true);
				txtPort.setEditable(true);
			}
		}
		if (ie.getSource() == chckbxPass)
		{
			if (passChecked = chckbxPass.isSelected())
			{
				txtPass.setEnabled(false);
				txtPass.setEditable(false);
			}
			else
			{
				txtPass.setEnabled(true);
				txtPass.setEditable(true);
			}
		}
	}
}
